package org.jbit.news.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jbit.news.biz.CommentBiz;
import org.jbit.news.biz.impl.CommentBizImpl;
import org.jbit.news.entity.Comment;

public class CommentAddServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//		request.setCharacterEncoding("utf-8");
//		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();	
		String cnid1 = request.getParameter("nid");
		System.out.println("11111"+cnid1);
//		int cnid=Integer.parseInt(cnid1);
		Comment comment = new Comment();
		comment.setCnid(cnid1);
		comment.setCcontent(request.getParameter("ccontent"));
		comment.setCdate(new SimpleDateFormat(
				"yyyy-MM-dd hh:mm:ss").format(new Date()));
		comment.setCip(request.getParameter("cip"));
		comment.setCauthor(request.getParameter("cauthor"));
		CommentBiz  commentBiz=new CommentBizImpl();
		int num=commentBiz.save(comment);
		String result="";
		if (num>0) {
			result="sucess";
			System.out.println(1);
		}else {
			result="评论添加失败！请联系管理员查找原因";
			System.out.println(2);
		}
			out.print("[{\"result\":\""+result+"\",\"cdate\":\""+comment.getCdate()+"\"}]");
			out.flush();
			out.close();
		
		
		
		
		
		
		
		
//		String cnid = request.getParameter("nid");
//		Comment comment = new Comment();
//		comment.setCnid(cnid);
//		comment.setCcontent(request.getParameter("ccontent"));
//		comment.setCdate(new SimpleDateFormat(
//				"yyyy-MM-dd hh:mm:ss").format(new Date()));
//		comment.setCip(request.getParameter("cip"));
//		comment.setCauthor(request.getParameter("cauthor"));
//		CommentBiz  commentBiz=new CommentBizImpl();
//		int num=commentBiz.save(comment);
//		request.getRequestDispatcher(
//				"../newspages/news_read.jsp?nid" + cnid).forward(request,
//				response);
	}
	
}
